package com.fire.tools;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;

public class Des {

    private final String DES = "DES";

    /**
     * 对 content 进行加密，返回的结果为加密后的字符串值
     *
     * @param content 要加密的内容
     * @param key     密钥
     * @return 加密后的字符串值
     * @throws Exception
     */
    public String encrypt(String content, String key) throws Exception {
        byte[] encrypt = encrypt(content.getBytes(), key.getBytes());
        String strs = new BASE64Encoder().encode(encrypt);//将字节数组经过base64编码转变为字符串String类型
        return strs;
    }

    /**
     * 对 content 的内容进行加密
     *
     * @param content byte[] 数组
     * @param keys    byte[] 数组
     * @return
     * @throws Exception
     */
    public byte[] encrypt(byte[] content, byte[] keys) throws Exception {
        // // 生成一个可信任的随机数源
        SecureRandom random = new SecureRandom();
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec desKeySpec = new DESKeySpec(keys);
        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory factory = SecretKeyFactory.getInstance(DES);
        SecretKey secretKey = factory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
        return cipher.doFinal(content);
    }

    /**
     * 解密
     *
     * @param content 要进行解密的密文
     * @param key     密钥
     * @return
     */
    public String decrypt(String content, String key) throws Exception {
        if (content == null) {
            return null;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes = decoder.decodeBuffer(content);
        byte[] decrypt = decrypt(bytes, key.getBytes());
        return new String(decrypt);
    }


    /**
     * 解密byte[] 数组
     *
     * @param content
     * @param keys
     * @return
     */
    public byte[] decrypt(byte[] content, byte[] keys) throws Exception {
        SecureRandom random = new SecureRandom();
        DESKeySpec desKeySpec = new DESKeySpec(keys);
        SecretKeyFactory factory = SecretKeyFactory.getInstance(DES);
        SecretKey secretKey = factory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
        return cipher.doFinal(content);
    }

    /**
     * 将 16 进制字符串转换成 16 进制数字数组  密钥传输交换阶段,用 DES 进行加解密前调用 得到的才是真正的密钥
     *
     * @param hexString DES 的明文
     * @return
     */
    public static byte[] asc2bin(String hexString) {
        byte[] hexbyte = hexString.getBytes();
        byte[] bitmap = new byte[hexbyte.length / 2];
        for (int i = 0; i < bitmap.length; i++) {
            hexbyte[i * 2] -= hexbyte[i * 2] > '9' ? 7 : 0;
            hexbyte[i * 2 + 1] -= hexbyte[i * 2 + 1] > '9' ? 7 : 0;
            bitmap[i] = (byte) ((hexbyte[i * 2] << 4 & 0xf0) | (hexbyte[i * 2 + 1] & 0x0f));
        }
        return bitmap;
    }

}